﻿package uc{
	
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	import flash.display.Loader;
	import flash.events.Event;
	import flash.media.Sound;
	import flash.display.Sprite;
	
	public class MultipleLoader extends Sprite{
		
		private var itemsToLoad:Array = new Array();
		private var loaded:uint;
		public var content:Object = new Object();
		
		public function MultipleLoader(a:Array=null){
			if(a){
				itemsToLoad = a;
			}
		}
		
		public function addItem(s:String){
			itemsToLoad.push(s);
		}
		
		public function addList(a:Array){
			for each(var i in a){
				itemsToLoad.push(i);
			}
		}
		
		public function addXmlList(x:XMLList){
			for each(var i in x){
				itemsToLoad.push(i.text());
			}
		}
		
		public function start(){
			load(itemsToLoad.shift());
		}
		
		public function reset(){
			itemsToLoad = null;
		}
		
		private function load(s:String){
			var typ:String = s.substr(s.length-4, s.length);
			
			if(typ==".jpg" || typ=="jpeg" || typ==".png" || typ==".gif"){
				
				var img:Loader = new Loader();
				img.contentLoaderInfo.addEventListener(Event.COMPLETE, loadComplete);
				img.load(new URLRequest(s));
				
			}else if(typ==".xml" || typ==".txt" || typ==".css"){
				
				var txt:URLLoader = new URLLoader();
				txt.addEventListener(Event.COMPLETE, loadComplete);
				txt.load(new URLRequest(s));
				
			}else if(typ==".mp3" || typ==".wav"){
				
				var snd:Sound = new Sound();
				snd.addEventListener(Event.COMPLETE, loadComplete);
				snd.load(new URLRequest(s));
				
			}else if(typ==".mpg" || typ=="mpeg" || typ==".avi" || typ==".swf"){
				
				var video:Loader = new Loader();
				video.contentLoaderInfo.addEventListener(Event.COMPLETE, loadComplete);
				video.load(new URLRequest(s));
				
			}
			
		}
		
		private function loadComplete(e:Event){
			e.target.removeEventListener(Event.COMPLETE, loadComplete);
			trace(e.target);
			
			if(e.target=="[object Bitmap]"){
				content["item"+(loaded++)] = e.target.content;
			}else if(e.target=="[object URLLoader]"){
				content["item"+(loaded++)] = e.target.data;
			}
			
			if(itemsToLoad.length){
				load(itemsToLoad.shift());
			}else{
				dispatchEvent(new Event("multipleLoaderComplete"));
			}
		}
	}
	
}